package com.hj.testdb;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hj.testdb.database.DBService;
import com.hj.testdb.database.User;
import com.hj.testdb.database.UserDao;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    UserDao userDao;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.btn_del)
    Button btnDel;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.btn_change)
    Button btnChange;
    @BindView(R.id.btn_clear)
    Button btnClear;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.ed_time)
    EditText edTime;
    @BindView(R.id.ed_name)
    EditText edName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        userDao = DBService.getInstance().getDaoSession().getUserDao();

    }

    @OnClick({R.id.btn_add, R.id.btn_del, R.id.btn_search, R.id.btn_change})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add:
                User user = new User();
                user.setName(edName.getText().toString());
                user.setCreatedAt(edTime.getText().toString());
                userDao.insert(user);//insertOrReplace  insertInTx一次插入多条
                break;
            case R.id.btn_del:
                User user1 = new User();
                user1.setName(edName.getText().toString());
                user1.setCreatedAt(edTime.getText().toString());


                QueryBuilder<User> qb = userDao.queryBuilder();


                break;
            case R.id.btn_search:

                tvText.setText("");
                List<User> users=userDao.queryBuilder()
                        .whereOr(UserDao.Properties.Name.eq(edName.getText().toString()),UserDao.Properties.CreatedAt.eq(edTime.getText().toString()))
                        .orderAsc(UserDao.Properties.CreatedAt).list();

                List<User> users1=userDao.queryBuilder().where(new WhereCondition.StringCondition(" name=? or created_at=?",edName.getText().toString(),edTime.getText().toString())).list();


                for (User u :users1) {
                    tvText.append("id"+u.getId()+"------"+u.getCreatedAt() + "----" + u.getName() + "\n");
                }
                break;
            case R.id.btn_change:


                break;
        }
    }

    @OnClick(R.id.btn_clear)
    public void onViewClicked() {
        tvText.setText("");
    }
}
